package config

import (
	"excel-exporter/types"
	"fmt"
)

// rewardProcessor 奖励表处理器 - 检查道具ID是否存在
func rewardProcessor(data []types.DataRow, exportType string, sheetConfig *types.Sheet) ([]types.DataRow, map[string]interface{}) {
	result := make([]types.DataRow, 0, len(data))
	extra := make(map[string]interface{})
	

	// 检查道具表依赖
	itemTableData, _ := loadJSONData("./output/server/json/item.json")
	if itemTableData == nil {
		fmt.Printf("Warn: 无法获取道具表数据，跳过道具ID检查\n")
	}
	// fmt.Printf("=========itemTable:%v", itemTableData)
	invalidItemIds := make([]string, 0)
	
	for _, row := range data {
		newRow := make(types.DataRow)
		
		// 复制原始数据
		for k, v := range row {
			newRow[k] = v
		}
		
		// 检查道具ID是否存在
		if itemTableData != nil {
			// 假设奖励表中有一个字段叫ItemId
			if itemId, exists := newRow["ItemId"]; exists {
				itemIdStr := convertToString(itemId)
				if itemIdStr != "" && !isItemIdValid(itemTableData, itemIdStr) {
					invalidItemIds = append(invalidItemIds, itemIdStr)
					fmt.Printf("Warn: 道具ID不存在: %s\n", itemIdStr)
				}
				// fmt.Printf("=============wtf:%s", itemIdStr)
			}
		}
		
		result = append(result, newRow)
	}
	
	
	extra["export_type"] = exportType
	extra["total_count"] = len(result)
	extra["processor"] = "exporter_reward"
	
	if len(invalidItemIds) > 0 {
		extra["invalid_item_ids"] = invalidItemIds
		extra["validation_warnings"] = len(invalidItemIds)
	}
	
	return result, extra
}

// isItemIdValid 检查道具ID是否有效
func isItemIdValid(itemTableData map[string]types.DataRow, itemID string) bool {
	if itemTableData == nil {
		return false
	}
	_, exists := itemTableData[itemID]
	return exists
}